package com.xixilala.zh_ad_style

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.ScrollView
import androidx.core.widget.NestedScrollView

class MyScrollView :NestedScrollView {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {

//        return super.onInterceptTouchEvent(ev)
        return false// 自己处理是否拦截，不根据父类判断
    }

    override fun onTouchEvent(ev: MotionEvent?): Boolean {
        ev?.let {
            for (index in 0 until childCount) {
                val cv = getChildAt(index)

                if (cv.id == R.id.scv && isTouchPointInView(cv,ev.x.toInt(),ev.y.toInt())) {
                    Logs.d("isTouch in imageView ${isTouchPointInView(cv,ev.x.toInt(),ev.y.toInt())}")
                    return true
                }
            }
        }

        return super.onTouchEvent(ev)
    }

    private fun isTouchPointInView(view: View?, x: Int, y: Int): Boolean {
        if (view == null) {
            return false
        }
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        val left = location[0]
        val top = location[1]
        val right: Int = left + view.getMeasuredWidth()
        val bottom: Int = top + view.getMeasuredHeight()
        //view.isClickable() &&
        return if (y >= top && y <= bottom && x >= left && x <= right
        ) {
            true
        } else false
    }
}